package com.gamatechno.solodestination;

import java.io.ByteArrayOutputStream;


import image.MyDataBase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TakePhotoActivity extends FragmentActivity implements OnClickListener{

	Bitmap b1;
	private ImageView imageview = null;
	private ImageView imageview2 = null;
	private Button btninsert = null;
	private TextView txtStamp1 = null;
	private TextView txtStamp2 = null;
	private Button btnretrive = null;
	private MyDataBase mdb = null;
	private SQLiteDatabase db = null;
	private Cursor c = null;
	private byte[] img = null;
	private static final String DATABASE_NAME = "ImageDb.db";
	public static final int DATABASE_VERSION = 1;
	Button btnTakePhoto;
	private static final int CAMERA_PIC_REQUEST = 1313;
	public static final String KEY_THUMB_URL = "thumb_url";
	byte[] bb;
	int i;
	
	Uri selectedImageUri;
	String selectedPath;
	ImageView preview;
	String pathofBmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_take_photo);
		Button b = (Button) findViewById(R.id.bGallery);
		Button bCam = (Button) findViewById(R.id.bCamera);
		
		preview = (ImageView) findViewById(R.id.preview);
		bCam.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent cameraIntent = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(cameraIntent, 100);
			}
		});

		
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				openGallery(10);
			}
		});
		
		
	}

	public void openGallery(int req_code) {

		Intent intent = new Intent();

		intent.setType("image/*");

		intent.setAction(Intent.ACTION_GET_CONTENT);

		startActivityForResult(
				Intent.createChooser(intent, "Select file to upload "),
				req_code);

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == RESULT_OK) {
			if (data.getData() != null) {
				selectedImageUri = data.getData();
			} else {
				Log.d("selectedPath1 : ", "Came here its null !");
				Toast.makeText(getApplicationContext(), "failed to get Image!",
						500).show();
			}

			if (requestCode == 100 && resultCode == RESULT_OK) {
				Bitmap photo = (Bitmap) data.getExtras().get("data");
				selectedPath = getPath(selectedImageUri);
				preview.setImageURI(selectedImageUri);
				Log.d("selectedPath1 : ", selectedPath);
				pathofBmp = Images.Media.insertImage(
						getContentResolver(), photo, "title", null);

				
				
				Button btnShare = (Button) findViewById(R.id.btnShare);
				btnShare.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Uri bmpUri = Uri.parse(pathofBmp);
						final Intent emailIntent1 = new Intent(
								android.content.Intent.ACTION_SEND);
						emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
						emailIntent1.setType("image/png");
						startActivity(emailIntent1);
					}
				});
			}

			if (requestCode == 10)

			{

				selectedPath = getPath(selectedImageUri);
				preview.setImageURI(selectedImageUri);
				Log.d("selectedPath1 : ", selectedPath);
				
				
				Button btnShare = (Button) findViewById(R.id.btnShare);
				btnShare.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Uri bmpUri = Uri.parse(selectedPath);
						final Intent emailIntent1 = new Intent(
								android.content.Intent.ACTION_SEND);
						emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						emailIntent1.putExtra(Intent.EXTRA_STREAM, bmpUri);
						emailIntent1.setType("image/png");
						startActivity(emailIntent1);
					}
				});

			}

		}

	}

	public String getPath(Uri uri) {

		String[] projection = { MediaStore.Images.Media.DATA };

		Cursor cursor = managedQuery(uri, projection, null, null, null);

		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

		cursor.moveToFirst();

		return cursor.getString(column_index);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}

}
